package cc.booku.owlbox.RsaUtil;

import android.content.Context;

import com.alibaba.fastjson.JSONObject;

import java.security.PrivateKey;

import cc.booku.owlbox.FileOperations.FileoOperations;

/**
 * Created by yangwenlong on 2020/6/11.
 */

public class Decrypt {

    private String ProfilePath = "/storage/emulated/0/OwlDatabase/Profile.txt";

    public static String RsaDecrypt(Context context, String ciphertext) {


        //解密
        //获取RSA_PRIVATE
        String jrsa = FileoOperations.readAssetsTxt(context, "TxtData/RSA");
        JSONObject obja1 = JSONObject.parseObject(jrsa);
        String RSA_PRIVATE = obja1.getString("RSA_PRIVATE");


        try {
            // 从字符串中得到私钥
            PrivateKey privateKey = RSAUtils.loadPrivateKey(RSA_PRIVATE);
            // 从文件中得到私钥
            // 因为RSA加密后的内容经Base64再加密转换了一下，所以先Base64解密回来再给RSA解密
            byte[] decryptByte = RSAUtils.decryptData(Base64Utils.decode(ciphertext), privateKey);
            String decryptStr = new String(decryptByte);
            //tv_decrypt.setText(decryptStr);
            return decryptStr;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }


    }

}
